package com.sinosoft.antiques.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sinosoft.antiques.common.ConfigReader;
import com.sinosoft.antiques.service.BasicService;
import com.sinosoft.antiques.service.ImportYxService;
import com.sinosoft.antiques.util.StringUtil;
import com.sinosoft.commontools.annotated.CustomDataAnnotation;
import com.sinosoft.commontools.annotated.rquestMethod;
import com.sinosoft.commontools.annotated.rquestMethod.ReqMode;
import com.sinosoft.commontools.record.Record;

public class CollectionManageController {

	@rquestMethod(MethodNote = ReqMode.All)
	@CustomDataAnnotation(isCustomRead = true)
	public Record addYxList_mass(HttpServletRequest request, HttpServletResponse response, Record data) {
		Record rs = new Record();
		rs.add("status", "OK");
		HttpSession session = request.getSession();
		Object userid = session.getAttribute("userid");
		if(StringUtil.isNullOrEmpty(userid)){
			rs.add("status", "login");
			rs.add("msg", "登陆超时！");
			return rs;
		}
		String yxpath1 = data.getStrValue("yxpath");
		String yxpath = data.getStrValue("yxpath");
		if(StringUtil.isNoEmpty(yxpath)){
			String importfilepath = ConfigReader.getValue("importfilepath");
			String firstchar = yxpath.substring(0, 1);
			if(firstchar.equals("/")||firstchar.equals("\\")){
				yxpath = importfilepath + yxpath;
			}else{
				yxpath = importfilepath + File.separator + yxpath;
			}
			File f = new File(yxpath);
			if(f.exists()){
				if(f.isDirectory()){
					File fs[] = f.listFiles();
					if(fs.length==0){
						rs.add("status", "failed");
						rs.add("msg", "文件路径（"+yxpath1+"）下不包含文件，请确认是否上传！");
						return rs;
					}
				}else{
					rs.add("status", "failed");
					rs.add("msg", "文件路径（"+yxpath1+"）不存在，请确认是否上传！");
					return rs;
				}
			}else{
				rs.add("status", "failed");
				rs.add("msg", "文件路径（"+yxpath1+"）不存在，请确认是否上传！");
				return rs;
			}
			ImportYxService.addYxList_mass(request, data);
			String.valueOf(userid);
			BasicService.insertTask(String.valueOf(userid), yxpath1);
		}else{
			rs.add("status", "failed");
			rs.add("msg", "文件路径（"+yxpath1+"）不能为空！");
			return rs;
		}
		return rs;
	}

}
